package com.artlongs.webapp.postman;

import com.artlongs.base.vo.BizRetVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import reactor.event.Event;

/**
 * Function：邮递员，收发信件啦。
 *
 * @version $Revision$ $Date$
 *          Date: 15-12-23
 *          Time: 上午11:07
 * @author: monda
 * @since 3.0
 */
@Service
public class PostmanImpl implements Postman {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public BizRetVo<Event> send(String toUrl,Event data) {
        BizRetVo bizRetVo = new BizRetVo();
        String dataStr = zip(data);
        if (StringUtils.isNotBlank(dataStr)) {
            String url = toUrl+ dataStr;
            logger.debug(dataStr);
            EventSend.to(url);
        }
        bizRetVo.setSuccess("ok");
        return bizRetVo;
    }

    @Override
    public BizRetVo<Event> receive(String data) {
        BizRetVo bizRetVo = new BizRetVo();
        bizRetVo.setItem(unzip(data));
        if (null != bizRetVo.getItem()) {
            bizRetVo.setSuccess("receive success.");
            logger.debug("接收成功:" +bizRetVo.getItem());
        }
        return bizRetVo;
    }
}
